﻿using System;
using System.Web.Mvc;
using demon;
using StudentInformationManagement.Models;

namespace StudentInformationManagement.Controllers
{
    public class ClassInfoController : Controller
    {
        // GET: ClassInfo
        public ActionResult Index()
        {
            ViewBag.Message = "班级信息页面";
            return View();
        }

        // POST: ClassInfo/Add
        [HttpPost]
        public ActionResult Add(string classid, string classname, string classmajor, string classstunum)
        {
            if (string.IsNullOrWhiteSpace(classid) || string.IsNullOrWhiteSpace(classname) || string.IsNullOrWhiteSpace(classmajor) || !Common.Checknum(classstunum))
            {
                ViewBag.ErrorMessage = "输入信息不规范！";
                return View();
            }

            string sqlStr = $"select count(*) from classinfo where classid='{classid}'";
            if (Common.ExecuteNum(sqlStr) == 1)
            {
                ViewBag.ErrorMessage = "该班级编号已被占用！";
                return View();
            }

            string sqlStr2 = $"Insert into classinfo(classid,classname,classmajor,classcount) Values ('{classid}','{classname}','{classmajor}','{classstunum}')";
            Common.ExecuteSql(sqlStr2);
            ViewBag.SuccessMessage = "班级信息添加成功！";
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Edit(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
